const {app, BrowserWindow, screen} = require('electron');
const path = require('path');
const url = require('url');

let win;
const args = process.argv.slice(1),
  serve = args.some(val => val === '--serve');

const createWindow = () => {

  const size = screen.getPrimaryDisplay().workAreaSize;

  win = new BrowserWindow({
    x: 0,
    y: 0,
    width: size.width,
    height: size.height,
    webPreferences: {
      nodeIntegration: true,
      allowRunningInsecureContent: serve,
    },
  });

  if (serve) {
    win.loadURL('http://localhost:4200');
    win.webContents.openDevTools();
  } else {
    // 移除菜单
    win.removeMenu();

    win.loadURL(url.format({
      pathname: path.join(__dirname, "dist/index.html"),
      protocol: 'file:',
      slashes: true
    }));

    win.webContents.on('did-fail-load', () => {
      win.loadURL(url.format({
        pathname: path.join(__dirname, 'dist/index.html'),
        protocol: 'file:',
        slashes: true
      }));
    });
  }

  win.on('closed', () => {
    win = null;
  });

  return win;
};

try {

  app.on('ready', createWindow);

  app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      app.quit();
    }
  });

  app.on('activate', () => {
    if (win === null) {
      createWindow();
    }
  });

} catch (e) {
  // Catch Error
  // throw e;
}
